/**
 * @filename noticeController.java
 */
package com.liupf.controller;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.liupf.domain.Notice;

/**
 * @description 公告管理
 * @author shevliu
 * @email shevliu@gmail.com Jul 28, 2012 3:20:49 PM
 */
public class NoticeController extends Controller {

	private Log log = LogFactory.getLog(this.getClass());

	public void index() {
		
	    String username=(String) this.getRequest().getSession().getAttribute("userName");
	    Page<Notice> page;
	    if(username.equals("admin"))
	    {
	        page = Notice.dao.paginate(getParaToInt(0, 1), 10,
                "select * ",
                "from notice where 1=1 order by id desc");
	    }
	    else{
	        String company = (String) this.getRequest().getSession().getAttribute("company");
	        
	        page = Notice.dao.paginate(getParaToInt(0, 1), 10,
	                "select * ",
	                "from notice where company=? order by id desc", company);
	    }
		
		setAttr("page", page);
		render("notice_index.ftl");
	}
	
	public void add(){
		render("notice_add.ftl");
	}
	
	public void save(){
		Notice notice = getModel(Notice.class);
		notice.put("time", new Date());
		notice.put("company", this.getRequest().getSession().getAttribute("company"));
		
		notice.save();
		redirect("/notice");
	}
	
	public void edit(){
		setAttr("notice", Notice.dao.findById(getParaToInt()));
		render("notice_edit.ftl");
	}
	
	public void update(){
		Notice notice = getModel(Notice.class);
		notice.update();
		redirect("/notice");
	}
	
	public void delete(){
		int id = getParaToInt(0);
		Db.update("delete from notice where id=?", id);
		redirect("/notice");
	}
	
}
